package com.hunlian.makelove.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;

import com.hunlian.makelove.R;
import com.hunlian.makelove.tools.DeviceTools;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DatePickerDialogNew extends Dialog {

	Context context;
	private Dialog dialog;
	private Handler mHandler;
	private DatePicker datePicker;
	private Button btnMakeSure;
	private Calendar calendar;
	private String date;
	private String currentDate;
	private Boolean flag = false;
	private String showDate = "";
	private Button mBtnCancel;

	public DatePickerDialogNew(Context context) {
		super(context);
		this.context = context;
		this.dialog = this;
	}

	public DatePickerDialogNew(Context context, int theme, int type,
			Handler mHandler,String showDate) {
		super(context, theme);
		this.context = context;
		this.mHandler = mHandler;
		this.showDate = showDate;
		this.dialog = this;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.datepicker_dialog);
		initView();
		initListener();

	}

	private void initView() {
		datePicker = (DatePicker) this.findViewById(R.id.datePicker);
		btnMakeSure = (Button) this.findViewById(R.id.btnMakeSure);
		mBtnCancel = (Button) this.findViewById(R.id.btn_cancel);
		calendar = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		currentDate = format.format(calendar.getTime());
		datePicker.setMinDate(DeviceTools.getYearMonthDayNum("1970-01-01"));
		if(TextUtils.isEmpty(showDate)){
			currentDate = format.format(calendar.getTime());
		}else{
			Date date;
			try {
				date = format.parse(showDate);
			} catch (ParseException e) {
				date = calendar.getTime();
			}
			currentDate = format.format(date);
		}
	}

	private void initListener() {
		/*SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Printout.println(format.format(calendar.getTime()));*/
		if(!TextUtils.isEmpty(showDate)){
			String[] dates = showDate.split("-");
			int year = Integer.parseInt(dates[0]);
			int monthOfYear = Integer.parseInt(dates[1])-1;
			int dayOfMonth = Integer.parseInt(dates[2]);
			date = year+"-"+monthOfYear+"-"+dayOfMonth;
			datePicker.init(year,monthOfYear,dayOfMonth,
					new DatePicker.OnDateChangedListener() {

						@Override
						public void onDateChanged(DatePicker view, int year,
								int monthOfYear, int dayOfMonth) {
							if(monthOfYear+1<10&&dayOfMonth<10){
								date = year + "-0" + (monthOfYear + 1) + "-0"
										+ dayOfMonth;
							}else if(monthOfYear+1<10&&dayOfMonth>9){
								date = year + "-0" + (monthOfYear + 1) + "-"
										+ dayOfMonth;
							}else if(monthOfYear+1>9&&dayOfMonth>9){
								date = year + "-" + (monthOfYear + 1) + "-"
										+ dayOfMonth;
							}else{
								date = year + "-" + (monthOfYear + 1) + "-0"
										+ dayOfMonth;
							}

						}
					});
		}else{
			int year = calendar.get(Calendar.YEAR)-15;
			int monthOfYear = calendar.get(Calendar.MONTH);
			int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
			date = year+"-"+monthOfYear+"-"+dayOfMonth;
			datePicker.init(year,monthOfYear,dayOfMonth,
					new DatePicker.OnDateChangedListener() {

						@Override
						public void onDateChanged(DatePicker view, int year,
								int monthOfYear, int dayOfMonth) {
							if(monthOfYear+1<10&&dayOfMonth<10){
								date = year + "-0" + (monthOfYear + 1) + "-0"
										+ dayOfMonth;
							}else if(monthOfYear+1<10&&dayOfMonth>9){
								date = year + "-0" + (monthOfYear + 1) + "-"
										+ dayOfMonth;
							}else if(monthOfYear+1>9&&dayOfMonth>9){
								date = year + "-" + (monthOfYear + 1) + "-"
										+ dayOfMonth;
							}else{
								date = year + "-" + (monthOfYear + 1) + "-0"
										+ dayOfMonth;
							}

						}
					});
		}

		btnMakeSure.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Message msg = new Message();
				Bundle bundle = new Bundle();

				msg.what = 1;
				bundle.putString("date", date);
				msg.setData(bundle);
				mHandler.sendMessage(msg);

			}
		});
		mBtnCancel.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});

	}
}
